package com.zhoug.zmui3.autocomplete;

import android.widget.Filter;

import com.zhoug.common3.interfaces.StringConverter;
import com.zhoug.common3.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;

/**
 * 自动补全数据加载过滤器
 *
 * @Author: zhoug
 * @Date: 2023-11-01
 * @Description:
 */
public abstract class ZMUIAutoCompleteFilter<T> extends Filter implements LifecycleObserver, StringConverter<T> {
    private static final String TAG = ">>>MyAutoCompleteFilter";
    private static boolean DEBUG = true;
    private ZMUIAutoCompleteAdapter<T> mAdapter;

    public ZMUIAutoCompleteFilter(LifecycleOwner lifecycleOwner, ZMUIAutoCompleteAdapter<T> adapter) {
        this.mAdapter = adapter;
        if (lifecycleOwner != null) {
            lifecycleOwner.getLifecycle().addObserver(new DefaultLifecycleObserver() {
                @Override
                public void onDestroy(@NonNull LifecycleOwner owner) {
                        cancelLoadList();
                }
            });
        }
    }

    /**
     * 子线程中获取数据
     *
     * @param constraint
     * @return
     */
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        if (DEBUG) {
            LogUtils.d(TAG, "performFiltering:constraint=" + constraint);
        }
        cancelLoadList();
        FilterResults filterResults = new FilterResults();
        String s = constraint != null ? constraint.toString().trim() : null;
        ArrayList<T> result = new ArrayList<>();
        loadListDataSyn(s, result);
        filterResults.count = result.size();
        filterResults.values = result;
        return filterResults;
    }

    /**
     * 主线程中刷新数据
     *
     * @param constraint
     * @param results
     */
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        if (mAdapter == null) return;
        if (results != null) {
            if (DEBUG) {
                LogUtils.d(TAG, "publishResults:count=" + results.count);
            }
            mAdapter.notifyDataSetChanged((List<T>) results.values);
        } else {
            mAdapter.notifyDataSetChanged(null);
        }
    }


    @Override
    public CharSequence convertResultToString(Object resultValue) {
        try {
            T t = (T) resultValue;
            return converterString(t);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.convertResultToString(resultValue);
    }

    /**
     * 同步请求数据 (如果明明获取到了数据但是没有显示窗口就可能是异步导致的)
     *
     * @param filter 过滤
     * @param Result 数据过
     * @return
     */
    protected abstract void loadListDataSyn(final @Nullable String filter, final @NonNull List<T> Result);

    /**
     * 取消请求数据
     */
    protected abstract void cancelLoadList();


}
